package top.chukongxiang.monitorservice.config.launcher;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import top.chukongxiang.monitorservice.mapper.ServerConfigMapper;
import top.chukongxiang.monitorservice.model.entity.ServerConfig;
import top.chukongxiang.monitorservice.service.HcNetSdkService;
import top.chukongxiang.monitorservice.service.ServerConfigService;
import top.chukongxiang.monitorservice.tools.H2Util;

import java.util.List;

/**
 * 根据现有配置启动
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/25 09:32
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SdkInitApplicationRunner implements ApplicationRunner {

    private final HcNetSdkService hcNetSdkService;

    private final ServerConfigService serverConfigService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        H2Util.execute(ServerConfigMapper.class, serverConfigMapper -> {
            List<ServerConfig> serverConfigs = serverConfigMapper.selectList(Wrappers.emptyWrapper());
            for (ServerConfig serverConfig : serverConfigs) {
                // 开始对设备执行播放
                try {
                    hcNetSdkService.play(serverConfig.getId());
                } catch (Exception e) {
                    log.error("[{}] 播放失败：{}", serverConfig.getName(), e.getMessage(), e);
                }
            }
        });
    }
}
